package demo.controller;

import java.security.Principal;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import demo.MyConfiguration;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@RestController
public class DemoGreetingController {

	@Autowired
	private MyConfiguration configuration;

	/*
	 * the service to provide the user credential
	 */
	@RequestMapping("/user")
	public Principal user(Principal user) {
		return user;
	}

	@PreAuthorize("hasRole('USER')")
	@RequestMapping(value = "/greeting", method = RequestMethod.GET)
	public String greeting() {
		return "(USER)Greeting from " + configuration.getServiceId();
	}

	@PreAuthorize("hasRole('ADMIN')")
	@RequestMapping(value = "/admingreeting", method = RequestMethod.GET)
	public String greeting2() {
		return "(ADMIN)Greeting from " + configuration.getServiceId();
	}
}
